<template>
  <div id="updateDialog">
    <el-dialog class="box" title="配置更新" :visible.sync="dialogShow" :before-close="handleClose">
      <el-form :model="form">
        <el-form-item label="上次更新状态：" :label-width="formLabelWidth">
          <el-input disabled v-model="form.state" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="上次更新时间：" :label-width="formLabelWidth">
          <el-input disabled v-model="form.date" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="上次更新内容：" :label-width="formLabelWidth">
          <el-input disabled v-model="form.detail" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="更新方式：" :label-width="formLabelWidth">
          <div v-if="form.state == '成功'" style="float: left;">
            <el-radio v-model="form.radio" @change="checkValue()" label="0">备份并更新</el-radio>
            <el-radio v-model="form.radio" @change="checkValue()" label="1">还原上次备份</el-radio>
          </div>
          <div v-if="form.state !== '成功'" style="float: left;">
            <el-radio v-model="form.radio" label="2">强制更新</el-radio>
          </div>
        </el-form-item>
        <el-form-item v-if="form.radio !== '1'" label="更新内容：" :label-width="formLabelWidth">
          <el-checkbox-group v-model="checkedDetails" @change="handleCheckedCitiesChange">
            <el-checkbox v-for="detail in details" :label="detail.key" :key="detail.key">{{detail.label}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :disabled="updateBtn" type="primary" @click="open2" >
          <update-massage v-if="this.updateBtn !== true"></update-massage>
          <span>确 定</span>
        </el-button>
        <el-button type="info" @click="handleClose()">取 消</el-button>
      </div>
    </el-dialog>
    <update-massage></update-massage>
  </div>
</template>

<script>
  import updateMassage from './updateMassage'
  import axios from 'axios'
  const detailsOptions = [
    { label: '联动策略', key: 1 },
    { label: '工程配置', key: 2 },
    { label: '安全时段', key: 3 }
    // { label: '心跳配置', key: 4 }
  ]
  export default {
    components: {
      updateMassage
    },
    data() {
      return {
        updateBtn: true,
        dialogFormVisible: false,
        form: {
          state: '',
          date: '',
          detail: '',
          radio: '0',
          // radioSingle: '3',
          checkedDetail: ''
        },
        formLabelWidth: '120px',
        checkAll: false,
        checkedDetails: [],
        details: detailsOptions,
        isIndeterminate: true,
        submitData: {
          access_token: window.localStorage.getItem('access_token'),
          type: 'update_get_last_status',
          update_type: 'conf',
          pid: localStorage.getItem('num')
        },
        updateConfData: {
          access_token: window.localStorage.getItem('access_token'),
          type: 'update_configuration',
          pid: localStorage.getItem('num'),
          data: {
            update_content: [],
            update_mode: 1
          }
        },
        UpdateInfo: ''
      }
    },
    created() {
      this.dialogShow = true
    },
    mounted() {
      this.queryUpdateInfo()
    },
    methods: {
      // 复选框监听
      checkValue() {
        var value = this.form.radio
        console.log(value)
        if (value === '1') {
          this.updateBtn = false
          this.checkedDetails = []
        } else {
          this.updateBtn = true
        }
      },
      handleCheckedCitiesChange(value) {
        this.updateConfData.data.update_content = value
        console.log(this.updateConfData.data.update_content)
        if (value.length === 0) {
          this.updateBtn = true
        } else {
          this.updateBtn = false
        }
      },
      // 子组件传值
      handleClose() {
        const dialogShow = this.dialogFormVisible // 所需要传的值
        this.$emit('giveFalse', dialogShow) // 传值的参数
      },
      // 获取上次更新信息
      queryUpdateInfo() {
        axios.post('/update', this.submitData, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.get_account_main = res.data.data
          this.form.state = this.get_account_main.update_status === 2 ? '成功' : '失败'
          this.form.date = this.get_account_main.update_time
          this.form.detail = this.get_account_main.update_content
          if (this.get_account_main.update_status === 1) {
            this.form.radio = '2'
          }
          console.log(res)
          console.log(this.form.state)
        })
          .catch((error) => {
            this.form.radio = '2'
            console.log(error)
          })
      },
      // 更新配置
      //      updateConfiguration() {
      //        this.updateConfData.data.updata_mode = parseInt(this.form.radio)
      //        axios.post('/update', this.updateConfData, {
      //          headers: {
      //            'Content-Type': 'application/x-www-form-urlencoded'
      //          }
      //        })
      //      },
      // 组织单个更新
      open2() {
        this.updateConfData.data.update_mode = parseInt(this.form.radio)
        // this.updateConfData.data.updata_mode = parseInt(this.form.radioSingle)
        this.confirmStr = ''
        if (this.updateConfData.data.update_mode === 1) {
          this.confirmStr = '确认进行还原上次备份？'
        } else if (this.updateConfData.data.update_mode === 0) {
          this.confirmStr = '确认进行备份并更新？'
        } else if (this.updateConfData.data.update_mode === 2) {
          this.confirmStr = '确认进行强制更新？'
        }
        this.$confirm(this.confirmStr, '提示', {
          cancelButtonClass: 'userCancel',
          confirmButtonClass: 'userConfirm',
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (this.updateConfData.data.update_mode === 1) {
            this.updateConfData.data.update_content = [1, 2, 3]
          }
          axios.post('/update', this.updateConfData, {
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
            }
          }).then((res) => {
            this.$message({
              type: 'info',
              message: '开始执行更新操作……'
            })
          }).catch((error) => {
            console.log(error)
            this.$message({
              type: 'error',
              message: error.data.errmsg
            })
          })
          this.handleClose()
        }).catch(() => {
          this.handleClose()
        })
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /*****************第一层弹窗*****************/
  .testColor{
    padding: 0;
    color: #fff;
  }
  /*****************第一层弹窗*****************/
  /deep/ .box > .el-dialog{
    width: 750px;
    min-height: 290px;
    margin-top: 30vh !important;
    border-radius: 4px;
  }
  /*****************第一层弹窗头部*****************/
  /deep/ .box > .el-dialog > .el-dialog__header{
    width: 750px;
    height: 39px;
    line-height: 43px;
    padding: 0;
    text-indent: 20px;
    font-weight: bolder;
    border-radius: 4px;
    background-color: #e3e6ee;
  }
  /deep/ .box > .el-dialog > .el-dialog__header > .el-dialog__headerbtn{
    top: 12px;
  }
  /deep/ .box > .el-dialog > .el-dialog__header > .el-dialog__headerbtn > i{
    font-size: 18px;
    font-weight: bolder;
  }
  /*****************第一层弹窗内容*****************/
  /deep/ .box > .el-dialog > .el-dialog__body{
    padding: 0 0 0 10px ;
    margin-top: 20px;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item{
    margin-bottom: 0;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item > .el-form-item__label{
    color: #69727E;
    font-weight: normal;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item > .el-form-item__content > .el-input > .el-input__inner{
    width: 610px;
    height: 30px;
  }
  /*****************第一层弹窗底部*****************/
  /deep/ .box > .el-dialog > .el-dialog__footer{
    border-top: 1px solid #e3e6ee;
  }
  /*****************底部按钮*****************/
  /deep/ .dialog-footer{
    padding-top: 8px;
  }
  /deep/ .dialog-footer > .el-button{
    width: 80px;
    height: 30px;
    padding: 0;
  }
</style>
